clear all
set maxvar 50000

//load up the bank strength measures 
use ../Intermediate/bank_ban_strength_yearly, clear
rename ban_intensity_new1 banintsty_n1

//list of ban strength variables
local ban_strength_list banintsty_n1 

keep BankName BankID year `ban_strength_list' 
compress
tempfile bans 
save `bans'

//load up the borrower bank exposures 
use ../Intermediate/exposure_shares, clear  
keep borrower_id BankID exp_share_*
rename exp_share_* *
//list of exposure variables 
ds borrower_id BankID, not
local exposure_list `r(varlist)'

joinby BankID using `bans', unmatched(both) 
keep if _merge == 3 
drop _merge 

order borrower_id BankID year 
sort borrower_id BankID year 

//generate bartick instruments 
qui foreach es in `exposure_list' {
	foreach bt in `ban_strength_list' {
		tempvar esbt 
		gen `esbt' = `es'*`bt'
		tempvar esbt0
		gen `esbt0' = (`es'>0)*`bt'
		gegen ss_`es'_`bt' = sum(`esbt'), by(borrower_id year)
		drop `esbt' `esbt0'
	}
}

keep borrower_id year ss_*
duplicates drop
compress
save ../Intermediate/shift_shares, replace
